import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        int port = 8888;
        try (Socket socket = new Socket(InetAddress.getLocalHost(), port);
             OutputStream outputStream = socket.getOutputStream();
             Scanner scanner = new Scanner(System.in);
        ) {
            System.out.println("链接到服务器，输入消息发送，输入“exit”退出");
            while (true) {
                String message = scanner.nextLine();
                if (message.equals("exit")) {
                    System.out.println("已断开连接");
                    break;
                }
                socket.getOutputStream().write((message+"\n").getBytes(StandardCharsets.UTF_8));
                outputStream.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}